package com.zhangmeng.deepseek.local.chat.controller;

import com.zhangmeng.deepseek.local.chat.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * @author zm
 * @date 2025/4/15 15:06
 * @version: 1.0
 */
@RequiredArgsConstructor
@RequestMapping("/ai")
@RestController
public class ChatController {

    @Autowired
    private final ChatClient chatClient;

    @Autowired
    private final ChatHistoryRepository chatHistoryRepository;

    @RequestMapping(value = "/chat", produces = "text/html;charset=utf-8")
    public Flux<String> chat(
            @RequestParam("prompt") String prompt,
            @RequestParam("chatId") String chatId
            ) {

        // 1.保存会话id
        chatHistoryRepository.save("chat", chatId);
        // 2.发送消息
        return chatClient.prompt().user(prompt)
                .advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .stream().content();
    }
}
